package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.*;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;


public class EditAppointmentPresenter implements ActionListener{
	private static DataBaseAccessor dba;
	private String patientName;
	private Appointment myAppointment;
	private IAppointmentView myView; 
	
	public EditAppointmentPresenter(Appointment myAppointment, IAppointmentView view, String patientName){
		this.myAppointment= myAppointment;
		this.myView= view;
		this.patientName= patientName;
		myView.addListener(this);
		
		dba = new DataBaseAccessor();
		dba.connectToDatabase();
		List<Doctor> docList = dba.getAllDoctors();
		myView.setDoctorsList(docList);
		
		this.setAppointmentInformation();
		
		this.editAppointmentProcess();
	}

	private void setAppointmentInformation() {
		if ( this.myAppointment != null ){
		}
	}

	@Override
	public void actionPerformed(ActionEvent action) {
		if ( action.getSource() != myView.getCancelButton()){
			dba= new DataBaseAccessor();
			dba.connectToDatabase();
		}
		
		if ( action.getSource() == myView.getCancelButton()){
			System.out.println("Exiting...");
			dba.closeConnectionToDatabase();
			myView.hideSelf();
		}
		else{
			updateAppointmentProcess();
		}
	}

	
	public void editAppointmentProcess(){
		
		JComboBox doctorName= myView.getDoctorName();
		JComboBox whenMonth= myView.getWhenMonth();
		JComboBox whenDay= myView.getWhenDay();
		JComboBox whenYear= myView.getWhenYear();
		JComboBox whenTime= myView.getWhenTime();
		JTextPane reasonText= myView.getReasonText();
		
		//doctorName.setSelectedItem(myAppointment.getRequestedDoc().getName().toString());
		doctorName.setSelectedItem("Test 2 Doctor");

		whenMonth.setSelectedIndex(myAppointment.getRequestedDate().get(Calendar.MONTH));
		whenDay.setSelectedIndex(myAppointment.getRequestedDate().get(Calendar.DAY_OF_MONTH));
		whenYear.setSelectedItem(Integer.toString(myAppointment.getRequestedDate().get(Calendar.YEAR)));
		//whenTime.setSelectedIndex(myAppointment.getRequestedDate().get(Calendar.MONTH));
		reasonText.setText(myAppointment.getReason());
		
		myView.showSelf();
		
	}

	public void updateAppointmentProcess(){
		
		String timeHour = myView.getWhenTime().getSelectedItem().toString();
		int hour = 0, minute = 0;
		if ( timeHour.equals("Any Time")){
			hour = 0;
			minute = 0;
		}else{
			String[] timeParts = timeHour.split(":");
			if ( timeParts.length == 2){
				hour = Integer.parseInt(timeParts[0].toString());
				if ( timeParts[1].contains("pm")){
					hour+=12;
				}
				String[] minuteParts = timeParts[1].split(" ");
				minute = Integer.parseInt(minuteParts[0]);
			}
		}
		
		@SuppressWarnings("deprecation")
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(myView.getWhenYear().getSelectedItem().toString());
		int month = Integer.parseInt(myView.getWhenMonth().getSelectedItem().toString());
		int date = Integer.parseInt(myView.getWhenDay().getSelectedItem().toString());
		cal.set(year, month, date, hour, minute);

		DataBaseAccessor db = new DataBaseAccessor();
		db.connectToDatabase();
		Doctor requestedDoc = db.find(Doctor.class, ((Doctor)myView.getDoctorName().getSelectedItem()).getId());
		this.myAppointment.setReason(myView.getReasonText().getText());
		this.myAppointment.setRequestedDate(cal);
		this.myAppointment.setRequestedDoc(requestedDoc);
		this.myAppointment.setPatientName(this.patientName);
		
		db.update(this.myAppointment);
		db.closeConnectionToDatabase();
		JOptionPane.showMessageDialog(null, "Appointment Change Submitted");
		myView.hideSelf();
	}
}
